#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"


# RECIPE START systemd
#
# depends: cross-pkg-config
# depends: meson
# depends: ninja-native
# depends: acl
# depends: kmod
# depends: libcap
# depends: libxcrypt
# depends: libgcrypt
# depends: util-linux
# depends: xz

MESON_CROSS_FILE="`pwd`/build/${TARGET}-cross.txt"

cat > "${MESON_CROSS_FILE}" <<-EOF
[host_machine]
system = 'linux'
cpu = '${TARGET%%-*}'
cpu_family = '${TARGET%%-*}'
endian = 'little'

[binaries]
c = '${TARGET}-gcc'
cpp = '${TARGET}-g++'
ar = '${TARGET}-ar'
strip = '${TARGET}-strip'
pkgconfig = '${xtools}${build_prefix}/bin/${TARGET}-pkg-config'
EOF

native_python="${xtools}${build_prefix}/bin/python3"

if ! $native_python -c 'import jinja2; import sys; sys.exit(0)' 2>/dev/null; then
	${native_python} -m pip install \
		--no-index \
		--find-links=file://`pwd`/localpypi \
	./localpypi/jinja-3.0.1.tar.gz
fi

mkdir -p build/systemd
cd build/systemd
env \
	CFLAGS="$CFLAGS" \
	LDFLAGS="$LDFLAGS" \
meson setup \
	--prefix=$prefix \
	--libdir=${prefix}/${target_libdir} \
	--localstatedir=/var/lib \
	-Dbuildtype=debugoptimized \
	-Dmode=release \
	-Dsplit-usr=false \
	-Dsplit-bin=true \
	-Dinstall-tests=false \
	-Dblkid=true \
	-Ddefault-dnssec=no \
	-Db_lto=false \
	-Drpmmacrosdir=no \
	-Dhomed=false \
	-Duserdb=false \
	-Dselinux=false \
	-Dman=false \
	-Ddns-servers="$RU_DNS_SERVERS" \
	--cross-file="$MESON_CROSS_FILE" \
	../../systemd

ninja -j${JOBS}
env DESTDIR="$stage" ninja install
cd ../..
rm -rf build/systemd

mkdir -p -m755 "${stage}${sysconfdir}/systemd/network"
cat > "${stage}${sysconfdir}/systemd/network/90-ethernet.network" <<-EOF
[Match]
Type=ether

[Network]
DHCP=ipv4
MulticastDNS=yes
EOF

# RECIPE END systemd
